﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Utility;
using VAdvantage.DataBase;
using VAdvantage.Logging;
using System.Data;
using VAdvantage.Classes;

namespace VAdvantage.Model
{
    public class MModuleRelatedInfo : VAdvantage.Model.X_AD_ModuleRelatedInfo
    {
        public MModuleRelatedInfo(Ctx ctx, int AD_ModuleRelatedInfo_ID, string trx)
            : base(ctx, AD_ModuleRelatedInfo_ID, trx)
        {
 
        }

        protected override bool BeforeSave(bool newRecord)
        {
            int c_bpartner_id = 0;
            string sql = "";
            if (GetC_BPartner_ID() != null)
            {
                c_bpartner_id = GetC_BPartner_ID();
            }

            int count = 0;
            if (c_bpartner_id > 0)
            {
                //Check prefix is bind to other BPartner
                sql = "SELECT count(prefix) " +
                      "FROM ad_moduleinfo MI inner join ad_modulerelatedinfo MRI on mi.ad_moduleinfo_id = mri.ad_moduleinfo_id " +
                      "WHERE mri.c_bpartner_id!=" + c_bpartner_id +
                                " AND mi.prefix=(Select prefix from ad_moduleinfo where ad_moduleinfo_id=" + GetAD_ModuleInfo_ID() + ")";

                count = Convert.ToInt32(DB.ExecuteScalar(sql));
                if (count > 0)
                {
                    log.SaveError("PrefixNotAvailable", "", false);
                    return false;
                }

                //Check assemblyname is bind to other BPartner and module name
                sql = "select count(Assemblyname) from ad_moduleinfo MI inner join ad_modulerelatedinfo MRI on mi.ad_moduleinfo_id = mri.ad_moduleinfo_id " +
                "where mri.c_bpartner_id!= " + c_bpartner_id +
                " and mi.assemblyname=(Select assemblyname from ad_moduleinfo where ad_moduleinfo_id=" + GetAD_ModuleInfo_ID() + ")";

                count = Convert.ToInt32(DB.ExecuteScalar(sql));
                if (count > 0)
                {
                    log.SaveError("AssemblynameAlreadyAssignedToOtherBPartnerModule", "", false);
                    return false;
                }

                sql = "select count(Assemblyname) from ad_moduleinfo MI inner join ad_modulerelatedinfo MRI on mi.ad_moduleinfo_id = mri.ad_moduleinfo_id " +
                         "where mri.c_bpartner_id= " + c_bpartner_id +
                         " and Mi.name!=(Select name from ad_moduleinfo where ad_moduleinfo_id=" + GetAD_ModuleInfo_ID() + ")" +
                          " and  mi.assemblyname=(Select assemblyname from ad_moduleinfo where ad_moduleinfo_id=" + GetAD_ModuleInfo_ID() + ")";

                count = Convert.ToInt32(DB.ExecuteScalar(sql));
                if (count > 0)
                {
                    log.SaveError("AssemblynameAlreadyAssignedToOtherModule", "", false);
                    return false;
                }

            }


            return base.BeforeSave(newRecord);
        }
    }
}
